package controllers.admin;

import models.System.Headline.SystemTitle;

import models.System.Headline.code;
import models.SystemModels.SystemUser;
import models.SystemModels.SystemUserDetails;
import models.send.SMSCODE;
import models.send.email;
import models.send.sms;
import net.sf.json.JSONObject;
import play.Logger;

import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2018-07-13.
 */
public class json extends Logo{
    /**
     * @大量发送邮件，获取总人数
     */
    public static void sendAll(){
        JSONObject json = new JSONObject();
        models.Models.Email emails = new models.Models.Email();
        Long id =Long.parseLong( params.get("id"));
        emails.title = params.get("title");
        emails.msg = params.get("msg");
        List<SystemUserDetails> systemUserDetailsList = SystemUserDetails.find("UserId",id).fetch();
        emails.account = systemUserDetailsList.get(0).Email;
        try{
            email.MsgEmail(emails);
            json.put("code",1);
            renderJSON(json);

        }catch (Exception ex){
            json.put("code",0);
            renderJSON(json);
        }
    }

    /**
     * @发送手机验证码
     */
    public static void IPone(){
        JSONObject json = new JSONObject();
        String IPone = params.get("IPone");
        if (IPone== null){
            json.put("code",false);
        }

        StringBuffer sb = new StringBuffer();
        Random rand = new Random();
        for(int i = 0;i<6;i++){
            sb.append(rand.nextInt(9));
        }
        code.SystemVerificationCode = sb.toString();
        if (code.SystemVerificationCode == null){
            json.put("code",false);
        }else{
            String content = String.format("%s%s%s",SystemTitle.SystemVerificationCodeLeft,code.SystemVerificationCode,SystemTitle.SystemVerificationCodeRight);
            sms sms = new sms();
            sms.phone=IPone;
            sms.msg =content;
            int code = SMSCODE.code(sms);
            if (code== 0){
                json.put("code",true);
            }else{
                json.put("code",false);
            }
        }
        renderJSON(json);
    }

    public static void IPones(){
        Long id = Long.parseLong(params.get("id"));
        String msg = params.get("msg");
        JSONObject json = new JSONObject();
        SystemUserDetails systemUser =SystemUserDetails.findById(id);
        if (msg == null){
            json.put("code",0);
        }else   {
            sms sms = new sms();
            sms.phone= systemUser.IPone;
            sms.msg =msg;
            int code = SMSCODE.code(sms);
            Logger.info(code+"");
            if (code== 1){
                json.put("code",1);
            }else{
                json.put("code",0);
            }
        }
        renderJSON(json);
    }
    /**
     * @验证验证码
     */
    public static void Verify(){
        JSONObject json= new JSONObject();
        String VerifyCode =  params.get("code");
        if (VerifyCode == null){
            json.put("code",false);
        }else{
            if (code.SystemVerificationCode.equals(VerifyCode)){
                json.put("code",true);
            }else{
                json.put("code",false);
            }
        }
        renderJSON(json);
    }

}
